package com.kuang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kuang.mapper.AstControllerHeartMapper;
import com.kuang.mapper.AstManageDeviceHeartMapper;
import com.kuang.pojo.AstControllerHeart;
import com.kuang.utils.ControllerLogUtils;
import com.kuang.utils.MQTTConnect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/single/down/")
@Slf4j
public class ImportSelectNewHeartController {

    @Resource
    AstControllerHeartMapper astControllerHeartMapper;
    @Resource
    AstManageDeviceHeartMapper astManageDeviceHeartMapper;

    Logger logger = Logger.getLogger(ImportSelectNewHeartController.class);

    @RequestMapping("ImportSelectNewControllerHeart")
    @ResponseBody
    public String ImportSelectNewControllerHeart(@RequestBody String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        Map check = check(jsonObject);
        //结果集
        Map result = new LinkedHashMap();
        String controllerAddress="/single/down/ImportSelectNewControllerHeart";
        String controllerName="查询IOT最新的设备心跳记录";
        String msg="";

        logger.info("接收："+json);

        if (check != null) {
            msg= MapUtils.getString(check, "500");
            result.put("success", "false");
            result.put("code", "500");
            result.put("msg", msg);
            result.put("data", new JSONArray());
//            int i = ControllerLogUtils.setErrorMsgLog("import", controllerAddress,
//                    controllerName,msg, json, JSON.toJSONString(result));
            return JSON.toJSONString(result);
        }

        List list=new LinkedList();
        String type = jsonObject.getString("type");
        if (StringUtils.equals("controller_heart",type)){
            list = astControllerHeartMapper.selectNews();
        }else if (StringUtils.equals("manage_heart",type)){
            list = astManageDeviceHeartMapper.selectNews();
        }

        if (list.size()==0) {
            msg="未查询到数据！";
            result.put("success", "false");
            result.put("code", "200");
            result.put("msg", msg);
            result.put("data", new JSONArray());
//            int i = ControllerLogUtils.setErrorMsgLog("import", controllerAddress,
//                    controllerName,msg, json, JSON.toJSONString(result));
            return JSON.toJSONString(result);
        }

        //查询成功
        msg="处理成功";
        result.put("success", "true");
        result.put("code", "200");
        result.put("msg",msg);
        result.put("data", list);
//        int i = ControllerLogUtils.setMsgLog("import", controllerAddress,
//                controllerName,msg, json, JSON.toJSONString(result));
        return JSON.toJSONString(result);

    }

    public Map check(JSONObject jsonMap) {
        Map error = new LinkedHashMap();
        String type = MapUtils.getString(jsonMap, "type", "");
        if (StringUtils.isBlank(type)) {
            error.put("500", "类型未填写");
            return error;
        }
        if (!StringUtils.contains("controller_heart,manage_heart",type)){
            error.put("500", "类型填写错误");
            return error;
        }

        return null;
    }

}
